package com.zhoushenlan.experiment12.service;

import com.zhoushenlan.experiment12.model.Student;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

    @Service
    public class StudentService {
                private List<Student> students = new ArrayList<>();

                public StudentService() {
                    // 初始化一些学生数据
                    students.add(new Student(1L, "Zhangsan", "Smith", "Zhangsan.smith@example.com", 20));
                    students.add(new Student(2L, "Lisi", "Mike", "Lisi@example.com", 22));
                    students.add(new Student(3L, "Wangwu", "Lihua", "Wangwu@example.com", 21));
                    students.add(new Student(4L, "Zhaoliu", "Helen", "Zhaoliu@example.com", 21));
                }

                // 获取所有学生
                public List<Student> findAllStudents() {
                    return students;
                }

                // 根据ID获取学生
                public Student findStudentById(Long id) {
                    return students.stream()
                            .filter(student -> student.getId().equals(id))
                            .findFirst()
                            .orElse(null);
                }

                // 创建新学生
                public Student createStudent(Student student) {
                    students.add(student);
                    return student;
                }

                // 更新学生信息
                public Student updateStudent(Long id, Student updatedStudent) {
                    for (int i = 0; i < students.size(); i++) {
                        if (students.get(i).getId().equals(id)) {
                            students.set(i, updatedStudent);
                            return updatedStudent;
                        }
                    }
                    return null;
                }

                // 删除学生
                public boolean deleteStudent(Long id) {
                    return students.removeIf(student -> student.getId().equals(id));
                }
            }